package com.pai4j.client.controller;
import com.pai4j.client.service.MomentService;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.config.annotation.SafeClick;
import com.pai4j.common.enums.ClickEvent;
import com.pai4j.common.helper.SessionHelper;
import com.pai4j.domain.vo.request.request.MomentSaveOrUpdateVO;
import com.pai4j.domain.vo.response.moment.MomentDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/client-service/moment")
public class MomentController {


    @Autowired
    private MomentService momentService;

    /**
     * 发布动态
     *
     * @return
     */
    @SafeClick(event = ClickEvent.PUBLISH_MOMENT, time = 2000, message = "动态发布中，请勿重复操作")
    @PostMapping("/publish")
    public PAIResponseBean<Long> publish(@RequestBody MomentSaveOrUpdateVO momentSaveOrUpdate) {

        momentSaveOrUpdate.validateRequestParam();

        String author = SessionHelper.getCurrentUserId();
        momentSaveOrUpdate.setAuthor(author);
        // 客户端ip
        String clientIp = SessionHelper.getCurrentClientIP();
        momentSaveOrUpdate.setIp(clientIp);
        Long momentId = momentService.publish(momentSaveOrUpdate);
        return PAIResponseBeanUtil.success(momentId);
    }

    /**
     * 朋友圈动态列表加载
     *
     * @param lastMomentId
     * @param size
     * @return
     */
    @GetMapping("/timeline")
    PAIResponseBean<List<MomentDetailVO>> timeline(@RequestParam Long lastMomentId,
                                                   @RequestParam(value = "size", defaultValue = "10") Integer size) {
        String userId = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success(momentService.timeline(userId, lastMomentId, size));
    }
}
